package informationretrieval;

import static informationretrieval.Constants.NAME_ID;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;

/**
 * 
 * SearchResult.java - a class that represent the result for some search query
 * 
 */
public class SearchResult {
	
	// Class variables
	private static final String TAG = SearchResult.class.getSimpleName();
	private ScoreDoc[] mScoreDocArray;
	private IndexSearcher mIndexSearcher;
	private int mTotalDocsInCorpus;

	/**
	 * Class constructor
	 * 
	 * @param scoreDocArray
	 *            And array of score per docs
	 * @param indexSearcher
	 *            A searcher
	 */
	public SearchResult(ScoreDoc[] scoreDocArray, IndexSearcher indexSearcher, int totalDocsInCorpus) {
		mScoreDocArray = scoreDocArray;
		mIndexSearcher = indexSearcher;
		mTotalDocsInCorpus = totalDocsInCorpus;
	}

	/**
	 * A method to get a document by his doc id
	 * 
	 * @param docId
	 *            The document id
	 * @return A document object
	 * @throws IOException
	 */
	public Document getDoc(int docId) throws IOException {
		return mIndexSearcher.doc(docId);
	}

	/**
	 * A method to get the document score array
	 * 
	 * @return The document score array
	 */
	public ScoreDoc[] getScoreDoc() {
		return mScoreDocArray;
	}

	public List<Integer> getQueryResultSet() {
		List<Integer> resultSet = new ArrayList<Integer>();
		try {
		for(ScoreDoc docId : mScoreDocArray) {
			int id = docId.doc;
			Document doc = getDoc(id);
			Integer resultDocId = Integer.parseInt(doc.getField(NAME_ID).stringValue());
			resultSet.add(resultDocId);
			//Collections.sort(resultSet);
		}
		} catch(Exception exception) {
			Log.error(TAG, exception, true);
		}
		return resultSet;
	}
}
